home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / util / Validator.js < prev   
Encoding:
Text File  |  2007-04-11  |  12.9 KB  |  445 lines

  1. /****i* SOURCE_FILE/INFO
  2.     *
  3.     * NAME
  4.     *  Validator.js
  5.     *
  6.     * USAGE
  7.     *  Part of Netobjects JavaScript Library.
  8.     *
  9.     * COPYRIGHT
  10.     *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.     *  All Rights Reserved.
  12.     *
  13.     *  This is an unpublished work protected by Website Pros, Inc.
  14.     *  as a trade secret, and is not to be used or disclosed except as
  15.     *  expressly provided in a written license agreement executed by
  16.     *  you and Website Pros, Inc.
  17.     *
  18.     *      <copyright@websitepros.com>
  19.     *
  20.     * NOTES
  21.     *  JavaScript code.
  22.     *
  23.     *****/
  24.  
  25. if (!IS.isModuleInitialized("IS.NOF.UTIL.Validator"))
  26.     /****h* NOF_JavaScript_Library/NOF.UTIL.Validator
  27.     *
  28.     * NAME
  29.     *  NOF.UTIL.Validator
  30.     *
  31.     * DESCRIPTION
  32.     * Input validator class.
  33.     *
  34.     * External dependencies: 
  35.     ****/
  36.  
  37.     /**
  38.     * Constructor
  39.     */
  40.     function NOF_Validator () {
  41.         this.__proto__ = NOF_Validator.prototype;
  42.         
  43.         this.isRequired = false;
  44.         this.isAlphaNum = false;
  45.         this.isFileName = false;
  46.         this.isAttributeValue = false;
  47.         
  48.         this.isAlhaNumNoSpace = false;
  49.         this.isEmail = false;
  50.         this.isLetter = false;
  51.         this.isAscii = false;
  52.         this.isDigit  = false;
  53.         this.isDouble = false;
  54.         this.isUSDouble = false;
  55.         this.isPercentage = false;
  56.         this.isAnyChars = false;
  57.         this.minLength = 0;
  58.         this.maxLength = "unlimited";
  59.         this.lengthOverLimit = false;
  60.         this.isLengthRequired = true;
  61.         this.invalidChar = '';
  62.     }
  63.     
  64.     function NOF_Validator_ProtoBuilder() {
  65.         var method = NOF_Validator.prototype;
  66.         
  67.         method.setIsRequired = function setIsRequired(isFieldNeeded) {
  68.             this.isRequired = isFieldNeeded;
  69.         }
  70.         method.setIsAlphaNum = function setIsAlphaNum (isAlpha) {
  71.             if (isAlpha) this.disableAll();
  72.             this.isAlphaNum = isAlpha;
  73.         }
  74.         method.setIsFileName = function setIsFileName (isFileName) {
  75.             if (isFileName) this.disableAll();
  76.             this.isFileName = isFileName;
  77.         }
  78.         method.setIsAttributeValue = function setIsAttributeValue (isAttributeValue) {
  79.             if (isAttributeValue) this.disableAll();
  80.             this.isAttributeValue = isAttributeValue;
  81.         }
  82.         
  83.         
  84.         method.setIsAlphaNumNoSpace = function setIsAlphaNumNoSpace (isAlphaNoSpace) {
  85.             if (isAlphaNoSpace) this.disableAll();
  86.             this.isAlphaNumNoSpace = isAlphaNoSpace;
  87.         }
  88.         method.setIsAnyChars = function setIsAnyChars (isAny) {
  89.             if (isAny) this.disableAll();
  90.             this.isAnyChars = isAny;
  91.         }
  92.         method.setIsEmail = function setIsEmail (isEmail) {
  93.             if (isEmail) this.disableAll();
  94.             this.isEmail = isEmail;
  95.         }
  96.         method.setIsAscii = function setIsAscii (isAscii) {
  97.             if (isAscii) this.disableAll();
  98.             this.isAscii = isAscii;
  99.         }
  100.         method.setIsLetter = function setIsLetter (isLttr) {
  101.             if (isLttr) this.disableAll();
  102.             this.isLetter = isLttr;
  103.         }
  104.         method.setIsDigit = function setIsDigit (isInt) {
  105.             if (isInt) this.disableAll();
  106.             this.isDigit  = isInt;
  107.         }
  108.         method.setIsDouble = function setIsDouble (isDbl) {
  109.             if (isDbl) this.disableAll();
  110.             this.isDouble = isDbl;
  111.         }
  112.         method.setIsUSDouble = function setIsUSDouble (isDbl) {
  113.             if (isDbl) this.disableAll();
  114.             this.isUSDouble = isDbl;
  115.         }
  116.         method.setIsPercentage = function setIsPercentage (isPrcnt) {
  117.             if (isPrcnt) this.disableAll();
  118.             this.isPercentage = isPrcnt;
  119.         }
  120.         method.setMinLength = function setMinLength (minValue) {
  121.             if (this.maxLength < minValue) {
  122.                 this.minLength = this.maxLength;
  123.                 this.maxLength = minValue;
  124.             }
  125.             else
  126.                 this.minLength = minValue;
  127.         }
  128.         
  129.         method.setMaxLength = function setMaxLength (maxValue) {
  130.             if (this.minLength > maxValue) {
  131.                 this.maxLength = this.minLength;
  132.                 this.minLength = maxValue;
  133.             }
  134.             else
  135.                 this.maxLength = maxValue;
  136.         }
  137.         
  138.         method.disableAll = function disableAll () {
  139.             this.isDouble = false;
  140.             this.isUSDouble = false;
  141.             this.isAlphaNum = false;
  142.             this.isFileName = false;
  143.             this.isAttributeValue = false;
  144.             this.isAlhaNumNoSpace = false;
  145.             this.isEmail  = false;
  146.             this.isLetter = false;
  147.             this.isDigit = false;
  148.             this.isPercentage = false;
  149.             this.isAnyChars = false;
  150.         }
  151.         
  152.         method.isValid = function isValid(str, re) {
  153.             if (re != null && !re.test(str)){
  154.                 return false;
  155.             }
  156.             return true;
  157.         }
  158.         
  159.         method.isEmptyString = function isEmptyString(str) {
  160.             var emptyRe = /\S+/;
  161.             if (!emptyRe.test(str))
  162.                 return true;
  163.             return false;
  164.         }
  165.         
  166.         method.isValidLength = function isValidLength (str) {
  167.             if (this.isLengthRequired) {
  168.                 if (str.length < this.minLength) {
  169.                     this.lengthOverLimit = true;
  170.                     return false;
  171.                 }
  172.                 if (this.maxLength != "unlimited" && str.length > this.maxLength) {
  173.                     this.lengthOverLimit = true;
  174.                     return false;
  175.                 }
  176.             }
  177.             return true;
  178.         }
  179.         
  180.         method.isValidDoubleLength = function isValidDoubleLength (str) {
  181.             if (this.isLengthRequired) {
  182.                 var index = str.indexOf (".");
  183.                 if (index == -1)
  184.                     index = str.indexOf (",");
  185.                 if (index == -1)
  186.                     index = str.length;
  187.                 
  188.                 var w = str.substring (0,index);
  189.                 var d = str.substring (index+1);
  190.                 if (d.length > this.minLength)
  191.                     return false;
  192.                 if (this.maxLength != "unlimited" && w.length > this.maxLength)
  193.                     return false;
  194.             }
  195.             return true;
  196.         }
  197.         
  198.         method.isValidAlphaNum = function isValidAlphaNum (str) {
  199.             var re = new RegExp ("[^0-9a-zA-Z-. \ufeff\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c6\u00c7\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d0\u00d1\u00d2\u00d3\u00d4\u00d5\u00d6\u00d9\u00da\u00db\u00dc\u00dd\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e6\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f0\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f9\u00fa\u00fb\u00fc\u00fd\u00fe\u00ff\u00df]",""); //└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓┘┌█▄▌αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷∙·√ⁿ²■ ▀
  200.             if (this.isValid (str, re)) {
  201.                 this.invalidChar = RegExp.lastMatch;
  202.                 return false;
  203.             }
  204.             
  205.             return this.isValidLength(str);
  206.         }
  207.         
  208.         method.isValidAlphaNumNoSpace = function isValidAlphaNumNoSpace (str) {
  209.             var re = new RegExp ("[^0-9a-zA-Z-.\ufeff\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c6\u00c7\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d0\u00d1\u00d2\u00d3\u00d4\u00d5\u00d6\u00d9\u00da\u00db\u00dc\u00dd\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e6\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f0\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f9\u00fa\u00fb\u00fc\u00fd\u00fe\u00ff\u00df]",""); //└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓┘┌█▄▌αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷∙·√ⁿ²■ ▀
  210.             if (this.isValid (str, re)) {
  211.                 this.invalidChar = RegExp.lastMatch;
  212.                 return false;
  213.             }
  214.             return this.isValidLength(str);
  215.         }
  216.         
  217.         method.isValidFileName = function isValidFileName(str) {
  218.             var re = /[\\\/:*?\"<>|]/;
  219.             if (this.isValid (str, re)) {
  220.                 this.invalidChar = RegExp.lastMatch;
  221.                 return false;
  222.             }
  223.             return this.isValidLength(str);
  224.         }
  225.  
  226.         method.isValidPhoneNumber = function (str) {
  227.             var re = /^(\+?\d{1,2})?([\s\-])?((\(\d+\))|(\d+))([\d\s.\-]*)$/;  // General: +<country code> (area) phone number
  228.             if (!this.isValid (str, re)) {
  229.                 this.invalidChar = RegExp.lastMatch;
  230.                 return false;
  231.             }
  232.             return this.isValidLength(str);
  233.         }
  234.     
  235.     
  236.         method.isValidAttributeValue = function isValidAttributeValue(str) {
  237.             var re = /["]/;
  238.                     if (this.isValid (str, re)) {
  239.                         this.invalidChar = RegExp.lastMatch;
  240.                         return false;
  241.                     }
  242.                     return this.isValidLength(str);
  243.             }
  244.         
  245.         method.isValidAnyChars = function isValidAnyChars (str) {
  246.             return this.isValidLength(str);
  247.         }
  248.         
  249.         method.isValidAscii = function isValidAscii (str) {
  250.             var idx = str.length;
  251.             for (i=0; i<idx; i++)
  252.                 if (str.charCodeAt(i) > 128) {
  253.                     this.invalidChar = str.charAt(i);
  254.                     return false;
  255.                 }
  256.             return this.isValidLength(str);
  257.         }
  258.         
  259.         method.isValidEmail = function isValidEmail (str, isInputValidation) {
  260.             var re = /[^0-9a-zA-Z-._@]/;
  261.             if (this.isValid (str, re)) {
  262.                 this.invalidChar = RegExp.lastMatch;
  263.                 return false;
  264.             }
  265.             if (isInputValidation) {
  266.                 re  = /^([a-zA-Z0-9_.-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  267.                 if (!this.isValid (str, re)) {
  268.                     return false;
  269.                 }
  270.             }
  271.             
  272.             var mail = str.split('@');
  273.             if (mail[0].length > 64)
  274.                 return false;
  275.             if (mail[1] && mail[1].length > 256)
  276.                 return false;
  277.             
  278.             return true;
  279.         }
  280.         
  281.         method.isValidLetter = function isValidLetter (str) {
  282.             //var re = /[^a-zA-Z]/;
  283.             var re = /[^a-zA-Z\ufeff\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c6\u00c7\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d0\u00d1\u00d2\u00d3\u00d4\u00d5\u00d6\u00d9\u00da\u00db\u00dc\u00dd\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e6\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f0\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f9\u00fa\u00fb\u00fc\u00fd\u00fe\u00ff\u00df]/;
  284.             if (this.isValid (str, re)) {
  285.                 this.invalidChar = RegExp.lastMatch;
  286.                 return false;
  287.             }
  288.             return this.isValidLength(str);
  289.         }
  290.         
  291.         method.isValidDouble = function isValidDouble (str, isInputValidation, isUSDouble) {
  292.             //debugger;
  293.             var re = /[^0-9.,]/;
  294.             if (isUSDouble)
  295.                 re = /[^0-9.]/;
  296.             
  297.             if (this.isValid (str, re)) {
  298.                 this.invalidChar = RegExp.lastMatch;
  299.                 return false;
  300.             }
  301.             
  302.             if (isUSDouble) {
  303.                 re = /.*\..*\..*/;
  304.                     if (this.isValid (str, re))
  305.                         return false;
  306.                 }
  307.         
  308.         if (isInputValidation) {
  309.             re = /[0-9]$/;
  310.             if (!this.isValid (str, re))
  311.                 return false;
  312.         }
  313.         return this.isValidDoubleLength(str);
  314.         }
  315.         
  316.         method.isValidUSDouble = function isValidUSDouble (str, isInputValidation) {
  317.             return this.isValidDouble(str, isInputValidation, true);
  318.         }
  319.         
  320.         method.isValidDigit = function isValidDigit (str) {
  321.             var re = /[^0-9]/;
  322.             if (this.isValid (str, re)) {
  323.                 this.invalidChar = RegExp.lastMatch;
  324.                 return false;
  325.             }
  326.             return this.isValidLength(str);
  327.         }
  328.         
  329.         method.isValidPercentage = function isValidPercentage (str) {
  330.             if (!this.isValidDouble(str))
  331.                 return false;
  332.             
  333.             if (str > 100)
  334.                 return false;
  335.             return true;
  336.         }
  337.         
  338.         method.isValidFTPAddress = function (str) {
  339.             if (str.toLowerCase().indexOf("ftp://") == 0)
  340.                 return true;
  341.             return false;
  342.         }
  343.     
  344.         method.isValidHTTPsAddress = function (str) {
  345.             if ((str.toLowerCase().indexOf("http://") == 0) || (str.toLowerCase().indexOf("https://") == 0))
  346.                 return true;
  347.             return false;
  348.         }
  349.     
  350.         method.validate = function validate(str, resourceHandler){
  351.             //debugger;
  352.             // check for empty or existing item
  353.             this.lengthOverLimit = false;
  354.             this.invalidChar = '';
  355.             var isNotOk = this.isEmptyString(str);
  356.             var propertyKey = "";
  357.             if (isNotOk) {
  358.                 if(this.isRequired) {
  359.                     if (resourceHandler) {
  360.                         propertyKey = 'validator.element.empty';
  361.                         return resourceHandler.getResourceProperty (propertyKey);
  362.                     }
  363.                     else
  364.                         return false;
  365.                 }
  366.                 else
  367.                     return true;
  368.             }
  369.             if (this.isAlphaNum) {
  370.                 isNotOk = !this.isValidAlphaNum (str);
  371.                 propertyKey = 'validator.element.invalidChar';
  372.             }
  373.             if (this.isFileName) {
  374.                 isNotOk = !this.isValidFileName (str);
  375.                 propertyKey = 'validator.element.invalidChar';
  376.             } else if (this.isAttributeValue) {
  377.                 isNotOk = !this.isValidAttributeValue (str);
  378.                 propertyKey = 'validator.element.invalidChar';
  379.             }
  380.             else if (this.isAlphaNumNoSpace) {
  381.                 isNotOk = !this.isValidAlphaNumNoSpace (str);
  382.                 propertyKey = 'validator.element.invalidChar';
  383.             }
  384.             else if (this.isAnyChars) {
  385.                 isNotOk = !this.isValidAnyChars (str);
  386.                 propertyKey = 'validator.element.invalidChar';
  387.             }
  388.             else if (this.isAscii) {
  389.                 isNotOk = !this.isValidAscii (str);
  390.                 propertyKey = 'validator.element.invalidChar';
  391.             }
  392.             else if (this.isEmail) {
  393.                 isNotOk = !this.isValidEmail (str, resourceHandler);
  394.                 propertyKey = 'validator.element.invalidChar';
  395.             }
  396.             else if (this.isLetter) {
  397.                 isNotOk = !this.isValidLetter (str);
  398.                 propertyKey = 'validator.element.invalidChar';
  399.             }
  400.             else if (this.isDouble) {
  401.                 isNotOk = !this.isValidDouble (str, resourceHandler);
  402.                 propertyKey = 'validator.element.invalidChar';
  403.             }
  404.             else if (this.isUSDouble) {
  405.                 isNotOk = !this.isValidUSDouble (str, resourceHandler);
  406.                 propertyKey = 'validator.element.invalidChar';
  407.             }
  408.             else if (this.isDigit) {
  409.                 isNotOk = !this.isValidDigit (str);
  410.                 propertyKey = 'validator.element.invalidChar';
  411.             }
  412.             else if (this.isPercentage) {
  413.                 isNotOk = !this.isValidPercentage (str);
  414.                 propertyKey = 'validator.element.invalidChar';
  415.             }
  416.             
  417.             if (isNotOk)
  418.                 if (resourceHandler != null)
  419.                     return resourceHandler.getResourceProperty (propertyKey);
  420.             else
  421.                 return false;
  422.             return true;
  423.         }
  424.         
  425.         method.validateInput = function validateInput (ch, str) {
  426.             str = str + ch;
  427.             return this.validate(str);
  428.         }
  429.         
  430.         method.setLengthRequired = function setLengthRequired (isRequired) {
  431.             this.isLengthRequired = isRequired;
  432.         }
  433.         
  434.         method.isLengthOverLimit = function isLengthOverLimit () {
  435.             return this.lengthOverLimit;
  436.         }
  437.     }
  438.     
  439.     NOF_Validator_ProtoBuilder();
  440.     //NOF.__proto__.Validator = NOF_Validator;
  441.     NOF.UTIL.__proto__.Validator = NOF_Validator;
  442.     
  443. }
  444.